<?php

class sfApplySettingsForm extends sfGuardUserProfileForm
{
  public function configure()
  {
    parent::configure();
    
    unset($this['validate'], $this['email']); //$this['user_id']

	$request = sfContext::getInstance()->getRequest();
	$this->_city_id = $request->getPostParameter('sfApplyApply[city_id]', $this->getObject()->getCityId());
	$zip_criteria  = ZipPeer::getZipCriteria($this->_city_id);

  	$years = range('1910', date('Y', time() - 86400 * 365 * 1));
	$this->widgetSchema['birthday'] = new sfWidgetFormDate(array('years' => array_combine($years, $years)));
	$this->validatorSchema['birthday'] = new sfValidatorDate(array('required' => false));
    
    $this->widgetSchema['country_id'] = new sfWidgetFormPropelChoice(array('model' => 'Country', 'add_empty' => true));    
	$this->widgetSchema['city_id'] = new myWidgetFormPropelJQueryAutocompleter(array('model' => 'City','url' => '/ajax/CityAutocomplete'));
    $this->widgetSchema['zip_id'] = new sfWidgetFormPropelChoice(array(
		'model' => 'Zip',
		'add_empty' => 'Select ZIP Code',
		'criteria' => $zip_criteria
	));    
    
    // if expanded  is true - will be radiobuttons
    $this->widgetSchema['gender'] = new sfWidgetFormChoice(array(
      'choices' => sfGuardUserProfilePeer::getGenderChoices(),
      'expanded' => true
    ));
    
    $this->setWidget('language_id', new sfWidgetFormPropelChoice(array('model' => 'Language', 'add_empty' => false, 'expanded' => true)));
    
    $this->setValidator('lname',
      new sfValidatorAnd(array(
        new sfValidatorString(array(
          'required' => true,
          'trim' => true,
          'min_length' => 4,
          'max_length' => 50
        )),
        // Usernames should be safe to output without escaping and generally username-like.
        new sfValidatorRegex(array(
          //'pattern' => '/^\w+$/'
          'pattern' => '/^[^<>&\|]+$/' 
        ), array('invalid' => 'Usernames must contain only letters, numbers and underscores.')),
      ))
    );
    $this->setValidator('gender', new sfValidatorChoice(array('choices' => array_keys(sfGuardUserProfilePeer::getGenderChoices()))));
    $this->setValidator('language_id' , new sfValidatorPropelChoice(array('model' => 'Language', 'column' => 'id')));

    // Disallow <, >, & and | in full names. We forbid | because 
    // it is part of our preferred microformat for lists of disambiguated
    // full names in sfGuard apps: Full Name (username) | Full Name (username) | Full Name (username)
    $this->setValidator('fname', new sfValidatorAnd(array(
      new sfValidatorString(array(
        'required' => true,
        'trim' => true,
        'min_length' => 4,
        'max_length' => 50)),
        new sfValidatorRegex(array(
          'pattern' => '/^[^<>&\|]+$/',
        ), array('invalid' => 'Full names may not contain &lt;, &gt;, | or &amp;.'))
    )));
    
	// Fix problem with update profile
    $this->validatorSchema->setPostValidator(
      new myValidatorPropelUnique(
            array('model' => 'sfGuardUserProfile', 'column' => array('phone'),'primary_key'=>'user_id'),
            array('invalid' => 'An account with that phone number already exists.')
    ));
        
    $this->widgetSchema->setNameFormat('sfApplySettings[%s]');
    $this->widgetSchema->setFormFormatterName('list');
    $this->widgetSchema->moveField('fname', sfWidgetFormSchema::FIRST);
    $this->widgetSchema->moveField('zip_id', sfWidgetFormSchema::AFTER, 'city_id');
    $this->widgetSchema->moveField('lname', sfWidgetFormSchema::AFTER, 'fname');

    $this->widgetSchema->setLabels(array('language_id' => 'Language'));
    $this->widgetSchema->setLabels(array('fname' => 'Firstname'));
    $this->widgetSchema->setLabels(array('lname' => 'Lastname'));
    
    $this->widgetSchema->setLabels(array('country_id' => 'Country'));
    $this->widgetSchema->setLabels(array('city_id' => 'City'));
    $this->widgetSchema->setLabels(array('zip_id' => 'Zip'));
    $this->widgetSchema->setLabels(array('username' => 'E-mail:'));
    
  }
}

